﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms.DataVisualization.Charting;
using System.Data;
using Cashier.utils;
using System.Drawing;

namespace Cashier.report
{
    public class MSChartUtil
    {
        /// <summary>
        /// 出货/进货统计报表
        /// </summary>
        /// <param name="chart">报表对象</param>
        /// <param name="reportType">报表统计时间类型</param>
        /// <param name="dateArr">X轴时间刻度</param>
        /// <param name="shipment_dt">Y轴绑定数据</param>
        public static void BillChartReport(Chart chart, ReportType reportType, int[] dateArr, DataTable shipment_dt)
        {
            try
            {
                String chartTitle = String.Empty;
                String rowField = String.Empty;
                String axisX = String.Empty;
                if (reportType == ReportType.Year)
                {
                    chartTitle = "年度出货金额统计";
                    rowField = "years";
                    axisX = "年份";
                }
                else if (reportType == ReportType.Month)
                {
                    chartTitle = "月出货金额统计";
                    rowField = "months";
                    axisX = "月份";
                }
                else if (reportType == ReportType.Day)
                {
                    chartTitle = "日出货金额统计";
                    rowField = "days";
                    axisX = "日";
                }

                chart.Series.Clear();
                chart.ChartAreas.Clear();
                chart.Titles.Clear();
                chart.Titles.Add(chartTitle);
                ChartArea chartArea = new ChartArea();
                chartArea.BackColor = chart.BackColor;
                chartArea.AxisX.Interval = 1; //X轴Label显示间隔
                chartArea.AxisX.IntervalOffset = 1; //X轴Label显示间隔偏移量，为1表示全部显示
                chartArea.AxisX.LabelStyle.IsStaggered = false; //X轴Label是否错开显示
                chartArea.AxisX.Title = axisX;
                chartArea.AxisY.Title = "金额(元)";
                chartArea.AxisX.LineWidth = 2;
                chartArea.AxisY.LineWidth = 2;
                chart.ChartAreas.Add(chartArea);

                Series shipmentSeries = new Series("销售出货");
                shipmentSeries.ChartType = SeriesChartType.Column;
                shipmentSeries.Color = Color.Red;
                shipmentSeries.BorderWidth = 2;
                shipmentSeries.ShadowOffset = 1;
                shipmentSeries.IsVisibleInLegend = false; //是否显示报表图例
                shipmentSeries.IsValueShownAsLabel = true;
                shipmentSeries.MarkerStyle = MarkerStyle.Diamond;
                shipmentSeries.MarkerSize = 8;
                shipmentSeries.LabelFormat = "0.00";
                chart.Series.Add(shipmentSeries);

                Series reshipmentSeries = new Series("销售退货");
                reshipmentSeries.ChartType = SeriesChartType.Column;
                reshipmentSeries.Color = Color.Yellow;
                reshipmentSeries.BorderWidth = 2;
                reshipmentSeries.ShadowOffset = 1;
                reshipmentSeries.IsVisibleInLegend = false; //是否显示报表图例
                reshipmentSeries.IsValueShownAsLabel = true;
                reshipmentSeries.MarkerStyle = MarkerStyle.Diamond;
                reshipmentSeries.MarkerSize = 8;
                reshipmentSeries.LabelFormat = "0.00";
                chart.Series.Add(reshipmentSeries);

                for (int i = 0; i < dateArr.Length; i++)
                {
                    int lopDate = dateArr[i];
                    double shipmentTotal = 0;
                    double reshipmentTotal = 0;
                    foreach (DataRow row in shipment_dt.Rows)
                    {
                        if (lopDate == Int32.Parse(row[rowField].ToString()))
                        {
                            if (!String.IsNullOrEmpty(row["result"].ToString()))
                            {
                                shipmentTotal = Double.Parse(row["result"].ToString());
                            }
                            if (!String.IsNullOrEmpty(row["retotal"].ToString()))
                            {
                                reshipmentTotal = Double.Parse(row["retotal"].ToString());
                            }
                            break;
                        }
                    }

                    shipmentSeries.Points.AddXY(lopDate, shipmentTotal);
                    reshipmentSeries.Points.AddXY(lopDate, reshipmentTotal);
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 员工销售统计
        /// </summary>
        /// <param name="chart"></param>
        /// <param name="shipment_dt"></param>
        public static void UserShipmentChartReport(Chart chart, DataTable shipment_dt)
        {
            chart.Series.Clear();
            chart.ChartAreas.Clear();
            chart.Titles.Clear();
            chart.Titles.Add("员工销售统计");
            ChartArea chartArea = new ChartArea();
            chartArea.BackColor = chart.BackColor;
            chartArea.AxisX.Interval = 1; //X轴Label显示间隔
            chartArea.AxisX.IntervalOffset = 1; //X轴Label显示间隔偏移量，为1表示全部显示
            chartArea.AxisX.LabelStyle.IsStaggered = false; //X轴Label是否错开显示
            chartArea.AxisX.Title = "月份";
            chartArea.AxisY.Title = "金额(元)";
            chartArea.AxisX.LineWidth = 2;
            chartArea.AxisY.LineWidth = 2;
            chart.ChartAreas.Add(chartArea);

            Series shipmentSeries = new Series("销售金额");
            shipmentSeries.ChartType = SeriesChartType.Column;
            shipmentSeries.Color = Color.Red;
            shipmentSeries.BorderWidth = 2;
            shipmentSeries.ShadowOffset = 1;
            shipmentSeries.IsVisibleInLegend = false; //是否显示报表图例
            shipmentSeries.IsValueShownAsLabel = true;
            shipmentSeries.MarkerStyle = MarkerStyle.Diamond;
            shipmentSeries.MarkerSize = 8;
            shipmentSeries.LabelFormat = "0.00";
            chart.Series.Add(shipmentSeries);

            for (int i = 0; i < 12; i++)
            {
                DataRow[] row = shipment_dt.Select("months=" + (i + 1));
                double value = 0;
                if (row.Length > 0)
                {
                    Decimal result = Convert.ToDecimal(row[0][0]);
                    value = Convert.ToDouble(result.ToString("f2"));
                }
                shipmentSeries.Points.AddXY(i + 1, value);
            }
        }
    }
}
